package cn.com.dycc.party_dues.remind.service;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import cn.com.dycc.party_dues.remind.dao.RemindDao;
import cn.com.dycc.party_dues.remind.entity.Remind;
import cn.com.dycc.party_dues.remind.param.RemindParam;
import cn.com.dycc.util.DateUtil;
import cn.com.dycc.util.Paging;

/**
 * 
 * Description: 党费自动提醒的设置管理业务层实现
 * Author: Liket
 * Version: 1.0
 * Create Date Time: 2018年11月15日 上午10:16:03.
 * Update Date Time: 
 * @see
 */
@Service("remindService")
public class RemindServiceImpl implements RemindService{
	
	@Resource
	private RemindDao remindDao;
	
	@Resource(name="paging")
	private Paging paging;
	
		
	public int addRemind(HttpServletRequest req, Remind remind)
			throws RemindNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String)session.getAttribute("org_id");
		String user_id = (String)session.getAttribute("id");
		if(org_id==null){
			throw new RemindNotFoundException("请重新登录");
		}
		if(remind==null){
			throw new RemindNotFoundException("请填写信息");
		}
		if(remind.getDues_month()==null||remind.getDues_month().trim().isEmpty()){
			throw new RemindNotFoundException("请填写党费年月");
		}
		if(remind.getDues_month().length()>6){
			throw new RemindNotFoundException("党费年月格式示例：201811");
		}
		Remind lastRemind = remindDao.findRemindByMonth(remind.getDues_month());
		if(lastRemind!=null){
			throw new RemindNotFoundException("该月份提醒已经存在，不能重复添加");
		}
		if(remind.getDate_b()==null||remind.getDate_b().trim().isEmpty()){
			throw new RemindNotFoundException("请填写开始时间");
		}
		if(remind.getDate_e()==null||remind.getDate_e().trim().isEmpty()){
			throw new RemindNotFoundException("请填写截止时间");
		}
		if(remind.getDt_limit()==null||remind.getDt_limit().trim().isEmpty()){
			throw new RemindNotFoundException("请填写党费缴纳截至日期");
		}
		
		remind.setOrg_id(org_id);
		remind.setCreate_date(DateUtil.getNowDate(DateUtil.TIME_FMT));
		remind.setOpt_user_id(user_id);
		
		int n = remindDao.addRemind(remind);
		if(n!=1){
			throw new RemindNotFoundException("系统繁忙，添加失败");
		}
		return n;
	}

	public int deleteRemind(HttpServletRequest req, String remind_id) throws RemindNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String)session.getAttribute("org_id");
		if(org_id==null){
			throw new RemindNotFoundException("请重新登录");
		}
		if(remind_id==null||remind_id.trim().isEmpty()){
			throw new RemindNotFoundException("请选择删除的提醒时间");
		}
		int n = remindDao.deleteRemind(remind_id,org_id);
		if(n!=1){
			throw new RemindNotFoundException("系统繁忙，删除失败");
		}
		return n;
	}

	public int updateRemind(HttpServletRequest req, Remind remind)
			throws RemindNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String)session.getAttribute("org_id");
		String user_id = (String)session.getAttribute("id");
		if(org_id==null){
			throw new RemindNotFoundException("请重新登录");
		}
		if(remind==null){
			throw new RemindNotFoundException("请填写信息");
		}
		if(remind.getDues_month()==null||remind.getDues_month().trim().isEmpty()){
			throw new RemindNotFoundException("请填写党费年月");
		}
		if(remind.getDues_month().length()>6){
			throw new RemindNotFoundException("党费年月格式示例：201811");
		}
		if(remind.getDate_b()==null||remind.getDate_b().trim().isEmpty()){
			throw new RemindNotFoundException("请填写开始时间");
		}
		if(remind.getDate_e()==null||remind.getDate_e().trim().isEmpty()){
			throw new RemindNotFoundException("请填写截止时间");
		}
		if(remind.getDt_limit()==null||remind.getDt_limit().trim().isEmpty()){
			throw new RemindNotFoundException("请填写党费缴纳截至日期");
		}
		
		Remind oldRemind = remindDao.findRemindById(remind.getRemind_id(),org_id);
		if(oldRemind==null){
			throw new RemindNotFoundException("系统繁忙，修改失败");
		}
		
		Remind newRemind = new Remind();
		
		if(!remind.getDues_month().equals(oldRemind.getDues_month())){
			Remind lastRemind = remindDao.findRemindByMonth(remind.getDues_month());
			if(lastRemind!=null){
				throw new RemindNotFoundException("该月份提醒已经存在，不能重复添加");
			}
			newRemind.setDues_month(remind.getDues_month());
		}
		if(!remind.getDate_b().equals(oldRemind.getDate_b())){
			newRemind.setDate_b(remind.getDate_b());
		}
		if(!remind.getDate_e().equals(oldRemind.getDate_e())){
			newRemind.setDate_e(remind.getDate_e());
		}
		if(!remind.getDt_limit().equals(oldRemind.getDt_limit())){
			newRemind.setDt_limit(remind.getDt_limit());
		}
		if(remind.getState()!=null){
			if(!remind.getState().equals(oldRemind.getState())){
				newRemind.setState(remind.getState());
			}
		}	
		
		newRemind.setOpt_user_id(user_id);		
		newRemind.setRemind_id(remind.getRemind_id());
		newRemind.setOrg_id(org_id);
		int n = remindDao.updateRemind(newRemind);
		if(n!=1){
			throw new RemindNotFoundException("系统繁忙，稍后再试");
		}
		return n;
	}

	public Remind findRemindById(HttpServletRequest req, String remind_id)
			throws RemindNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String)session.getAttribute("org_id");
		if(org_id==null){
			throw new RemindNotFoundException("请重新登录");
		}
		if(remind_id==null||remind_id.trim().isEmpty()){
			throw new RemindNotFoundException("提醒时间不存在");
		}
		Remind remind = remindDao.findRemindById(remind_id,org_id);
		if(remind==null){
			throw new RemindNotFoundException("系统繁忙，查询失败");
		}
		return remind;
	}

	public List<Remind> findRemindByParam(HttpServletRequest req,RemindParam param)
			throws RemindNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String)session.getAttribute("org_id");
		if(org_id==null){
			throw new RemindNotFoundException("请重新登录");
		}
		Integer page = param.getPage();
		Integer limit = param.getLimit();
		Integer start = null;
		if(limit==null&&page!=null){
			limit = paging.getPageSize();
			start=(page-1)*limit;
		}
		if(page==null){
			start = null;
			limit = null;
		}
		if(limit!=null&&page!=null){
			start = limit*(page-1);
		}		
		param.setStart(start);	
		param.setOrg_id(org_id);
		List<Remind> list = remindDao.findRemindByParam(param);
		return list;
	}

	public String countRemindByParam(HttpServletRequest req,RemindParam param)
			throws RemindNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String)session.getAttribute("org_id");
		if(org_id==null){
			throw new RemindNotFoundException("请重新登录");
		}
		param.setOrg_id(org_id);
		String count = remindDao.countRemindByParam(param);
		return count;
	}

	
}
